<?php
//echo "dgdfgd";
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );
@session_start();

$_MAMBOTS->registerFunction( 'jpMetaEdit', 'jpmetaedit' );

global $mosConfig_absolute_path, $mosConfig_live_site;
if(!class_exists('JEConfig')) require_once( $mosConfig_absolute_path.'/administrator/components/com_jp/joomlaequipment.config.php');
$_SESSION['REQ_URI'] = $_SERVER['REQUEST_URI'];


function jpmetaedit( ) 
{
    $insertdef = JEConfig::get("general.jp_enable", 'com_jp');
    $revtitle = JEConfig::get("Meta.jp_reverse_title", 'com_jp');

    if (!$insertdef) return FALSE;
    
    if($revtitle == 'All except published')
    {
        jpRevTitle();
    }
    
    global $database, $mainframe, $mosConfig_MetaKeys, $mosConfig_MetaDesc, $_VERSION;

    $url = $_SESSION['REQ_URI'];
    
    if ($GLOBALS['mosConfig_sef']) {
        $w = '`sef`';
    } else {
        $w = '`original`';
    }

    $tempPU = parse_url($GLOBALS['mosConfig_live_site']);
    if (!empty($tempPU['path']))
    {
        $sitePath = $tempPU['path'];
    } else
    {
        $sitePath = '';
    }
    
    $url = substr_replace($url, '', 0, strlen($sitePath));
    
    $url =  $database->getEscaped($url);
    $sql = "SELECT `newmeta_title`,
                   `newmeta_description`,
                   `newmeta_keywords`,
                   `new_title`,
                   `robots_follow`,
                   `robots_index`,
                   `title`,
                   `meta_title`,
                   `meta_description`,
                   `meta_keywords`,
                   `published`
              FROM `#__jp_pages`
             WHERE ".$w." = '{$url}' ";

    
    $database->setQuery($sql);
    $result = $database->query();
    if(! $result) {
        echo $database->stderr();
        exit;
    }
    $rows = $database->loadAssocList();
    
/*    
    if($rows[0]['redirect'])
    {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: {$rows[0][redirect]}");
        exit();
    }
*/    
    $mainframe->addMetaTag( 'Generator', $_VERSION->PRODUCT . ' - ' . $_VERSION->COPYRIGHT);

    $insertdef    = JEConfig::get("Meta.jp_insert_default", 'com_jp');
    $defmetakwd   = JEConfig::get("Meta.jp_meta_kwd", 'com_jp');
    $defmetadescr = JEConfig::get("Meta.jp_meta_descr", 'com_jp');
    $defmetatitle = JEConfig::get("Meta.jp_meta_title", 'com_jp');
    $defpagetitle = $defmetatitle;
          
    if (count($rows)==0) return;    
          
    if ($insertdef)
    {
        if (!$rows[0]['meta_title']){$mainframe->addMetaTag('title', $defmetatitle);}
        if (!$rows[0]['meta_keywords']){$mainframe->addMetaTag('keywords', $defmetakwd);}
        if (!$rows[0]['meta_description']){$mainframe->addMetaTag('description', $defmetadescr);}
        if (!$rows[0]['title']){$mainframe->setPageTitle( $defpagetitle ); }
        //if (!$rows[0]['title']){}                        
    }

    if (($rows[0]['published'])==0) return;
    unset($mainframe->_head['meta']);
    
     
    $mainframe->addMetaTag('title', ( $rows[0]['newmeta_title'] ? $rows[0]['newmeta_title'] : $defmetatitle ));
    $mainframe->addMetaTag('keywords', ( $rows[0]['newmeta_keywords'] ? $rows[0]['newmeta_keywords'] : $defmetakwd ) );
    $mainframe->addMetaTag('description', ($rows[0]['newmeta_description'] ? $rows[0]['newmeta_description'] : $defmetadescr ) );
    $mainframe->addMetaTag( 'Generator', $_VERSION->PRODUCT . ' - ' . $_VERSION->COPYRIGHT);          
    $mainframe->addMetaTag('robots', $rows[0]['robots_index'].', '.$rows[0]['robots_follow']);
    if(JEConfig::get("general.jp_meta_verify", 'com_jp'))  $mainframe->addMetaTag('verify-v1', JEConfig::get("general.jp_meta_verify", 'com_jp'));
    
    if(JEConfig::get("Meta.jp_meta_author", 'com_jp'))
        $mainframe->addMetaTag('author', JEConfig::get("Meta.jp_meta_author", 'com_jp'));
    if(JEConfig::get("Meta.meta_copyright", 'com_jp'))
        $mainframe->addMetaTag('copyright', JEConfig::get("Meta.jp_meta_copyright", 'com_jp'));
    $mainframe->_head['title'] = ($rows[0]['new_title'] ? $rows[0]['new_title'] : $defpagetitle );    
    //$mainframe->setPageTitle();    
  
}
function jpRevTitle()
{
    global $mainframe, $mosConfig_sitename;
    
    $t = $mainframe->_head['title'];
    
    $t = str_replace($mosConfig_sitename, "", $t);
    $t = preg_replace("/^-/", "", trim($t));
    $t = preg_replace("/-$/", "", trim($t));
    $t ? $t .= " - ".$mosConfig_sitename : $t = $mosConfig_sitename;
    
    $mainframe->_head['title'] = $t;
}
?>